package com.nuc.seckill.service.impl;

import cn.hutool.core.lang.Assert;
import com.nuc.seckill.base.CommonQueryBean;
import com.nuc.seckill.dao.SeckillProductsDao;
import com.nuc.seckill.enums.ProductsEnum;
import com.nuc.seckill.model.SeckillProducts;
import com.nuc.seckill.service.ProductsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 石一歌
 * @date 2022-08-05 21:52
 */
@Service("productsService")
@Slf4j
public class ProductsServiceImpl implements ProductsService {
    @Resource
    private SeckillProductsDao seckillProductsDao;

    @Override
    public SeckillProducts selectByPrimaryKey(Long id) {
        return seckillProductsDao.selectByPrimaryKey(id);
    }

    @Override
    public int insert(SeckillProducts record) {
        return seckillProductsDao.insert(record);
    }

    @Override
    public int updateByPrimaryKeySelective(SeckillProducts record) {
        return seckillProductsDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<SeckillProducts> list4Page(SeckillProducts record, CommonQueryBean query) {
        return seckillProductsDao.list4Page(record, query);
    }

    @Override
    public long count(SeckillProducts record) {
        return seckillProductsDao.count(record);
    }

    @Override
    public List<SeckillProducts> list(SeckillProducts record) {
        return seckillProductsDao.list(record);
    }

    /**
     * 唯一新增
     *
     * @param record 记录
     * @return java.lang.Long
     * @author 石一歌
     * @date 2022/8/5 22:24
     */
    @Override
    public Long uniqueInsert(SeckillProducts record) {
        SeckillProducts existItem = null;
        try {
            record.setCreateTime(new Date());
            record.setUpdatedTime(new Date());
            record.setIsDeleted(ProductsEnum.IS_NOT_DELETED.getValue());
            record.setStatus(ProductsEnum.STATUS_IS_OFFLINE.getValue());
            record.setSaled(0);
            existItem = findByProductPeriodKey(record.getProductPeriodKey());
            if (existItem != null) {
                return existItem.getId();
            } else {
                seckillProductsDao.insert(record);
            }
        } catch (Exception ex) {
            if (ex.getMessage().contains("Duplicate entry")) {
                return existItem.getId();
            } else {
                log.error(ex.getMessage(), ex);
                throw new RuntimeException(ex.getMessage());
            }
        }
        return null;
    }

    /**
     * 通过外键查找商品
     *
     * @param productPeriodKey 外键
     * @return com.nuc.seckill.model.SeckillProducts
     * @author 石一歌
     * @date 2022/8/5 22:27
     */
    @Override
    public SeckillProducts findByProductPeriodKey(String productPeriodKey) {
        Assert.isTrue(!StringUtils.isEmpty(productPeriodKey));
        SeckillProducts item = new SeckillProducts();
        item.setProductPeriodKey(productPeriodKey);
        List<SeckillProducts> list = seckillProductsDao.list(item);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }
    }
}
